{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Temperature Sensor 1.2\n",
    "\n",
    "This example shows how to use the [Grove Temperature Sensor v1.2](http://wiki.seeedstudio.com/Grove-Temperature_Sensor_V1.2/). You will also see how to plot a graph using matplotlib. The Grove Temperature sensor produces an analog signal, and requires an ADC. \n",
    "\n",
    "A Grove Temperature sensor and Pynq Grove Adapter, or Pynq Shield is required. The Grove Temperature Sensor, Pynq Grove Adapter, and Grove I2C ADC are used for this example. \n",
    "\n",
    "You can read a single value of temperature or read multiple values at regular intervals for a desired duration.\n",
    "\n",
    "At the end of this notebook, a Python only solution with single-sample read functionality is provided."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Load overlay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Read single temperature\n",
    "This example shows on how to get a single temperature sample from the Grove TMP sensor.\n",
    "\n",
    "The Grove ADC is assumed to be attached to the GR4 connector of the StickIt. The StickIt module is assumed to be plugged in the 1st PMOD labeled JB. The Grove TMP sensor is connected to the other connector of the Grove ADC.\n",
    "\n",
    "Grove ADC provides a raw sample which is converted into resistance first and then converted into temperature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.2 degree Celsius\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "from pynq.lib.pmod import Grove_TMP\n",
    "from pynq.lib.pmod import PMOD_GROVE_G4\n",
    "\n",
    "tmp = Grove_TMP(base.PMODB,PMOD_GROVE_G4)\n",
    "temperature = tmp.read()\n",
    "print(float(\"{0:.2f}\".format(temperature)),'degree Celsius')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Start logging once every 100ms for 10 seconds\n",
    "Executing the next cell will start logging the temperature sensor values every 100ms, and will run for 10s. You can try touch/hold the temperature sensor to vary the measured temperature.\n",
    "\n",
    "You can vary the logging interval and the duration by changing the values 100 and 10 in the cellbelow. The raw samples are stored in the internal memory, and converted into temperature values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEICAYAAABfz4NwAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XuYXHWd5/H3J024NEQuScuQWzeO6DxRuUgPXkGDAxui\nI84wOLBhJrPqZrjooOuoceL4PDv75HlYnWVHZxDN4CWzaUEUUEYYFVwUbwt0MAghXCKmA+GSTkKE\nGAdI8t0/zqnpk6Iup7qqum6f1/PUU1Wnzjn1O6erz/f87ooIzMzMprU6AWZm1h4cEMzMDHBAMDOz\nlAOCmZkBDghmZpZyQDAzM8ABwcyKSFokaWOr02FTzwHBXkTSeZLukPQbSVvT1xdLUgvTNF/Srswj\n0vQV3p/aqrTVS9LB6fHMncLvvFDSnvTcPSPpbkmLJrGfayR9ohlptKnngGD7kfRh4DPAp4HfAY4G\nLgTeBBxYZpu+ZqcrIjZHxGGFR7r4hMyyHzU7DZMl6YAp+I7J/A1+kJ7LI4Grga9LOqzKNtbFHBDs\nP0g6HPg74OKI+EZEPBuJn0fEkoh4Ll3vK5KulHSzpN8ACyUdLulfJI1LGpP0CUnTJB0kaaekV2e+\nZ0DSbyW9NH3/Dknr0vV+Kun4Sab/EEn/IOlRSU9K+kdJB6WfLZK0MU3XNklbJL1d0rsk/VLS9jQY\nFvZ1maSrJV0n6VlJd0l6VebzeZK+le7rEUkXFm37VUlfk/QscJ6kN6U5rZ2SHpf0vzOB4vb0+cH0\njv1d6R38rZl97peLSO/MP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nw5LT67StKh9OC5iIgdwN8Dm4En\ngF9HxPfowXORUe7YJ3U9baeAYICkw4DrgA9GxDPZzyJpI9z17YQlvQPYGhFlx63qlXNBMrzMa4Er\nI+Ik4DcUFYn0yrmQ9LvAh0iC5GzgUEkXZNfplXNRSiOOvZ0CwhZgXub93HRZz5A0nSQYjETE9eni\npyQdk35+DLC1VembQm8C3ilpE0nR4emS1tCb5+Ix4LGIuCN9/w2SANGL52IY+GlEjEfEC8D1wBvp\nzXNRUO7YJ3U9baeAcBdwnKRjJR1IUiFyY4vTNGUkiaSceENEXJ756EZgafp6KfCtqU7bVIuIj0fE\n3IgYIvkd/N+IuIDePBdPAo9KemW66G3A/fTguQAeBF4vqT/9f3kbSV1bL56LgnLHfiNwnqSDJB0L\nHAfcWXVvEdE2D2Ax8BDwS2BFq9Mzxcf+ZpLs3i+AdeljMTCTpPXAw8CtwFGtTusUn5e3At9OX/fk\nuQBOBEbT38Y3gSN7+Fx8jCQg3gf8H+CgXjkXwNUkdScvkOQc31vp2IEV6bX0QeCsPN/hoSvMzAxo\nryIjMzNrIQcEMzMDHBDMzCzlgGBmZoADgpmZpRwQzMwMcEAwM7PU/wdyT/UypO5lbwAAAABJRU5E\nrkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2fa057b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import time\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "tmp.set_log_interval_ms(100)\n",
    "tmp.start_log()\n",
    "# Change input during this time\n",
    "time.sleep(10)\n",
    "tmp_log = tmp.get_log()\n",
    "\n",
    "plt.plot(range(len(tmp_log)), tmp_log, 'ro')\n",
    "plt.title('Grove Temperature Plot')\n",
    "min_tmp_log = min(tmp_log)\n",
    "max_tmp_log = max(tmp_log)\n",
    "plt.axis([0, len(tmp_log), min_tmp_log, max_tmp_log])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 4. A Pure Python class to exercise the AXI IIC Controller inheriting from PMOD_IIC\n",
    "This class is ported from http://wiki.seeedstudio.com/Grove-Temperature_Sensor/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "from math import log\n",
    "from pynq.lib.pmod import PMOD_GROVE_G3\n",
    "from pynq.lib.pmod import PMOD_GROVE_G4\n",
    "from pynq.lib import Pmod_IIC\n",
    "\n",
    "class Python_Grove_TMP(Pmod_IIC):\n",
    "    \"\"\"This class controls the grove temperature sensor.\n",
    "    \n",
    "    This class inherits from the PMODIIC class.\n",
    "    \n",
    "    Attributes\n",
    "    ----------\n",
    "    iop : _IOP\n",
    "        The _IOP object returned from the DevMode.\n",
    "    scl_pin : int\n",
    "        The SCL pin number.\n",
    "    sda_pin : int\n",
    "        The SDA pin number.\n",
    "    iic_addr : int\n",
    "        The IIC device address.\n",
    "    \n",
    "    \"\"\"\n",
    "    def __init__(self, pmod_id, gr_pins, model = 'v1.2'): \n",
    "        \"\"\"Return a new instance of a grove OLED object. \n",
    "        \n",
    "        Parameters\n",
    "        ----------\n",
    "        pmod_id : int\n",
    "            The PMOD ID (1, 2) corresponding to (PMODA, PMODB).\n",
    "        gr_pins: list\n",
    "            The group pins on Grove Adapter. G3 or G4 is valid.\n",
    "        model : string\n",
    "            Temperature sensor model (can be found on the device).\n",
    "            \n",
    "        \"\"\"\n",
    "        if gr_pins in [PMOD_GROVE_G3, PMOD_GROVE_G4]:\n",
    "            [scl_pin,sda_pin] = gr_pins\n",
    "        else:\n",
    "            raise ValueError(\"Valid group numbers are G3 and G4.\")\n",
    "            \n",
    "        # Each revision has its own B value\n",
    "        if model == 'v1.2':\n",
    "            # v1.2 uses thermistor NCP18WF104F03RC\n",
    "            self.bValue = 4250\n",
    "        elif model == 'v1.1':\n",
    "            # v1.1 uses thermistor NCP18WF104F03RC\n",
    "            self.bValue = 4250\n",
    "        else:\n",
    "            # v1.0 uses thermistor TTC3A103*39H\n",
    "            self.bValue = 3975\n",
    "        \n",
    "        super().__init__(pmod_id, scl_pin, sda_pin, 0x50)\n",
    "        \n",
    "        # Initialize the Grove ADC         \n",
    "        self.send([0x2,0x20]);   \n",
    "        \n",
    "    def read(self):\n",
    "        \"\"\"Read temperature in Celsius from grove temperature sensor.\n",
    "        \n",
    "        Parameters\n",
    "        ----------\n",
    "        None\n",
    "        \n",
    "        Returns\n",
    "        -------\n",
    "        float\n",
    "            Temperature reading in Celsius.\n",
    "        \n",
    "        \"\"\"\n",
    "        val = self._read_grove_adc()\n",
    "        R = 4095.0/val - 1.0\n",
    "        temp = 1.0/(log(R)/self.bValue + 1/298.15)-273.15\n",
    "        return temp\n",
    "        \n",
    "    def _read_grove_adc(self):       \n",
    "       self.send([0])\n",
    "       bytes = self.receive(2)\n",
    "       return 2*(((bytes[0] & 0x0f) << 8) | bytes[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.54 degree Celsius\n"
     ]
    }
   ],
   "source": [
    "from pynq import PL\n",
    "\n",
    "# Flush IOP state\n",
    "PL.reset()\n",
    "\n",
    "py_tmp = Python_Grove_TMP(base.PMODB, PMOD_GROVE_G4)\n",
    "temperature = py_tmp.read()\n",
    "print(float(\"{0:.2f}\".format(temperature)),'degree Celsius')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
